package com.zgc.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zgc.entity.RoleMenu;
import com.zgc.mapper.RoleMenuMapper;
import com.zgc.service.RoleMenuService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu>
        implements RoleMenuService {
    @Override
    public void saveAll(String roleid, List menuids) {
        if(StringUtils.isEmpty(roleid)||null==menuids ||menuids.size()==0){
            return ;
        }
//1.先删除之之前添加过的
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new
                LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.eq(RoleMenu::getRoleId,roleid);
        int count2 = this.count(roleMenuLambdaQueryWrapper);
        if(count2 > 0){
            baseMapper.delete(roleMenuLambdaQueryWrapper);
        }
//2.菜单是数组，拆分完成之后再保存
        for (int i = 0; i <menuids.size() ; i++) {
// 如果是根节点（1，2，3），不用保存到中间表中
            List<String> list=new ArrayList<>();
            list.add("1");
            list.add("2");
            list.add("3");
            if(list.contains(menuids.get(i))){
                continue;
            }
            RoleMenu roleMenu=new RoleMenu();
            roleMenu.setRoleId(Long.parseLong(roleid));
            roleMenu.setMenuId(Long.parseLong((String) menuids.get(i)));
            baseMapper.insert(roleMenu);
        }
    }
}